package com.home.common.utils.qr_code;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Hashtable;

/**
 * Description: 二维码生成工具类
 * Created by 王大宸 on 2020-10-28 18:15
 * Created with IntelliJ IDEA.
 */
public class QrCodeUtil {
    /**
     * 黑色
     */
    private static final int BLACK = 0xFF000000;
    /**
     * 白色
     */
    private static final int WHITE = 0xFFFFFFFF;
    /**
     * 二维码的宽
     */
    private static final int WIDTH = 900;
    /**
     * 二维码的高
     */
    private static final int HEIGHT = 900;


    /***
     * 功能说明：二维码下载
     *
     * @author 王大宸
     * @date 2020/10/28 18:18
     * @param content     内容
     * @param fileName    二维吗名称
     * @param response    response
     * @return void
     */
    public static void downloadQrcode(String content, String fileName, HttpServletResponse response) throws Exception {
        //转换字符，避免中文乱码问题
        content = new String(content.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        // 矫错级别
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //创建比特矩阵(位矩阵)的QR码编码的字符串
        BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hintMap);
        // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
        int matrixWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
        // 使用比特矩阵画并保存图像
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < matrixWidth; i++) {
            for (int j = 0; j < matrixWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }
        /* 将image转换成流 */
        response.setHeader("Content-Type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".jpg");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();
        response.getOutputStream().write(imageInByte);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
}
